package com.lqr.emoji;

import android.text.TextUtils;

import com.blankj.utilcode.util.ActivityUtils;
import com.google.gson.JsonObject;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.file.FileInfo;
import com.zy.openchat.core.model.file.FileTaskInfo;
import com.zy.openchat.core.model.sticker.GetStickersEvent;
import com.zy.openchat.core.model.sticker.StickerInfo;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * CSDN_LQR
 * 贴图管理类
 */
public class StickerManager {

    private static StickerManager instance;

    //数据源
    private List<StickerCategory> stickerCategories = new ArrayList<>();
    private Map<String, StickerCategory> stickerCategoryMap = new HashMap<>();
//    private Map<String, Integer> stickerOrder = new HashMap<>();

    public static StickerManager getInstance() {
        if (instance == null) {
            synchronized (StickerManager.class) {
                if (instance == null) {
                    instance = new StickerManager();
                }
            }
        }
        return instance;
    }

    public StickerManager() {
//        initStickerOrder();
//        loadStickerCategory();
    }

    public void refreshStickerCategory(){
        stickerCategories.clear();
        stickerCategoryMap.clear();
        loadStickerCategory();
    }

    private void loadStickerCategory() {
        TelegramManager.getTelegramManager().getFavoriteStickers(ActivityUtils.getTopActivity(), new TgResultBlock<List<StickerInfo>>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, List<StickerInfo> obj) {
                stickerCategories.clear();
                stickerCategoryMap.clear();
                if(obj!=null&&obj.size()>0){
                    List<StickerItem> list = new ArrayList<>();
                    for (int i = 0;i<obj.size();i++){
                        StickerInfo stickerInfo = obj.get(i);
                        list.add(new StickerItem(FIRST_CATE,String.valueOf(i),stickerInfo));
                    }
                    StickerCategory category = new StickerCategory(FIRST_CATE, FIRST_CATE, true, 0,list);
                    stickerCategories.add(category);
                    stickerCategoryMap.put(FIRST_CATE, category);
                    EventBus.getDefault().post(new GetStickersEvent());
                }

            }

            @Override
            public void onTimeout(Map<String, Object> request) {

            }
        });
    }


    public boolean hasSticker(long remoteFileId){
        return hasStickerReturnInfo(remoteFileId)!=null;
    }

    public StickerInfo hasStickerReturnInfo(long remoteFileId){
        if(remoteFileId<=0){
            return null;
        }
        if(stickerCategories!=null&&stickerCategories.size()>0){
            List<StickerItem> stickerItems = stickerCategories.get(0).getStickers();
            if(stickerItems!=null&&stickerItems.size()>0){
                for(StickerItem stickerItem:stickerItems){
                    if(stickerItem.getStickerInfo()!=null&&remoteFileId == stickerItem.getStickerInfo().getAnimationFileId()){
                        return stickerItem.getStickerInfo();
                    }
                }
                return null;
            }else{
                return null;
            }
        }else{
            return null;
        }
    }

    public final String FIRST_CATE = "category1";

    public synchronized List<StickerCategory> getStickerCategories() {
        return stickerCategories;
    }

    public synchronized StickerCategory getCategory(String name) {
        return stickerCategoryMap.get(name);
    }

    public String getStickerBitmapUri(StickerInfo stickerInfo) {
        if(stickerInfo!=null){
            if(stickerInfo.isAnimationDownloaded()){
                return stickerInfo.localAnimationPath();
            }else{
                TelegramManager.getTelegramManager().downloadFile("sticker_"+stickerInfo.getAnimationFileId(), stickerInfo.getAnimationFileId(), 0, TelegramManager.FileType.FileType_Message_Animation);
                return "";
            }
        }else{
            return "";
        }
//        String path = getStickerBitmapPath(categoryName, stickerName);
//        return "file://" + path;
    }

    public int updateStickerFile(FileTaskInfo task, FileInfo file){
        if (task != null && file != null && !TextUtils.isEmpty(task.id) && task.id.startsWith("sticker_") && stickerCategories!=null &&stickerCategories.size()>0) {
            String[] idArray = task.id.split("_");
            if(idArray!=null&&idArray.length>=2){
                String animationFileId = idArray[1];
                List<StickerItem> stickerInfos = stickerCategories.get(0).getStickers();
                if(stickerInfos!=null&&stickerInfos.size()>0&&!TextUtils.isEmpty(animationFileId)){
                    for(int i = 0;i<stickerInfos.size();i++){
                        StickerInfo stickerInfo = stickerInfos.get(i).getStickerInfo();
                        if(animationFileId.equals(String.valueOf(stickerInfo.getAnimationFileId()))){
                            stickerInfo.updateAnimationFile(file);
                            return i;
                        }
                    }
                }
            }
        }
        return -1;
    }


}
